package com.zc.controller.newController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.GoodsDetail;
import com.zc.dto.ResultDto;
import com.zc.entity.Role;
import com.zc.entity.Users;
import com.zc.initConstructor.GlobalData;
import com.zc.service.permissionService.PermissionService;
import com.zc.vo.LoginInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.lang.model.element.NestingKind;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@CrossOrigin
@Controller
public class WebloginController extends BaseController {
    @Resource
    private PermissionService permissionBiz;

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultDto login(LoginInfo login, Model model) {
        ResultDto result = new ResultDto();
        Subject subject = SecurityUtils.getSubject();
        String username = login.getUsername();
        String password = GlobalData.decoratePassword(login.getPassword());
        String YZMCode = login.getYzmCode();
        String identification = login.getIdentification();
        LOG.debug(login.toString());
        //验证验证码的有效性
        if (!jedis.exists(identification)) {
            result.setResultData(false, 1, "验证码已经是失效,请重新输入", null);
            return result;
        } else if (!YZMCode.equalsIgnoreCase(jedis.get(identification))) {
            result.setResultData(false, 2, "验证码输入错误,请重新输入", null);
        }

        AuthenticationToken token = new UsernamePasswordToken(username, password);
        Users userInfo = null;
        try {
            subject.login(token);
            userInfo = (Users) subject.getPrincipal();
            String jsonUserinfo = JSONObject.toJSONString(userInfo);
            jedis.set(login.getConfirmMID(), jsonUserinfo);
            result.setResultData(true, 0, "登入成功", null);
        } catch (Exception e) {
            LOG.error("login error::,{}", e);
            result.setResultData(false, 999, "账户名和密码不匹配,请重新输入 ", null);
        }
        return result;
    }


    @CrossOrigin
    @RequestMapping(value = "/nav", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto getNav(String confirmMID) {
        String userInfo = jedis.get(confirmMID);
        Users users = JSON.parseObject(userInfo, Users.class);
        String nav = jedis.get(users.getUsername() + "nav");
        ResultDto result = new ResultDto();
        if (nav != null && !("".equals(nav))) {
            result.setNav(nav);
            result.setResultData(true, 0, "拉取nav成功", null);
        } else {
            result.setResultData(false, 1, "您的权限失效,请联系您的上级", null);
        }
        return result;
    }

    @CrossOrigin
    @RequestMapping(value = "/getAllRole", method = RequestMethod.POST)
    @ResponseBody
    public ResultDto getAllRole(String confirmMID) {
        ResultDto result = new ResultDto();
        try {
            String userInfo = jedis.get(confirmMID);
            Users users = JSON.parseObject(userInfo, Users.class);
            List<Role> allRole = permissionBiz.findAllRole(users.getRoleId());
            result.setResultData(true,0,"获取角色信息成功",allRole);
        } catch (Exception e) {
          LOG.error("createUser error:: ,{}",e);
          result.setResultData(false,1,"获取角色信息失败",null);
        }
        return result;
    }

    @CrossOrigin
    @RequestMapping(value = "/createUser",method = RequestMethod.POST)
    @ResponseBody
    public ResultDto createUser(Users user){
        ResultDto result = new ResultDto();
        try {
            permissionBiz.saveUser(user);
            result.setResultData(true,0,"新建用户成功",null);
        }catch (Exception e){
            LOG.error("createUser error :: e,{}",e);
            result.setResultData(false,1,"新建用户失败",null);
        }
        return result;
    }
}

